﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterviewQuestions.Anagrams
{
    /// <summary>
    /// 
    /// </summary>
    public class Anagrams
    {
        /// <summary>
        /// god and Dog are anagrams. 123dog and 123god are not.
        /// </summary>
        /// <param name="a">string a</param>
        /// <param name="b">string b</param>
        /// <returns>true if anagrams. Otherwise, return false. </returns>
        public Boolean Execute(string a, string b)
        {
            if (a.Length != b.Length)
            {
                return false;
            }

            int[] array = new int[26];
            char[] array_a = a.ToUpper().ToCharArray();
            
            foreach (char chr in array_a)
            {
                if (chr-'A'<0 || chr-'Z'>0)
                {
                    return false;
                }
                array[chr-'A']++;
            }
            char[] array_b = b.ToUpper().ToCharArray();
            foreach (char chr in array_b)
            {
                if (chr - 'A' < 0 || chr- 'Z'>0)
                {
                    return false;
                }
                array[chr-'A']--;
            }
            foreach (int item in array)
            {
                if (item!=0)
                {
                    return false;
                }
            }

            return true;
        }

        public Boolean ExecuteQuicksort(string a, string b)
        {
            if (a.Length != b.Length)
            {
                return false;
            }

            char[] array_a = a.ToUpper().ToCharArray();
            Array.Sort(array_a);
            char[] array_b = b.ToUpper().ToCharArray();
            Array.Sort(array_b);

            for (int i=0; i < a.Length; i++ )
            {
                if (array_a[i] != array_b[i])
                {
                    return false;
                }
            }

            return true;
        }
    }
}
